package cn.gok.server.service.impl;

import cn.gok.common.dao.backstage.PictureMapper;
import cn.gok.common.entity.backstage.Picture;
import cn.gok.server.service.PictureService;
import cn.gok.utils.DateUtil;
import cn.gok.utils.ImageUtil;
import cn.gok.utils.UserContext;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author: DxlinY
 * @apiNote: 图片对象服务层
 * @date: 2020/10/10
 * @time: 11:11
 */
@Service
@Slf4j
public class PictureServiceImpl implements PictureService {

    @Autowired
    private PictureMapper pictureMapper;

    /**
     * 模块名，方便区分文件内容
     */
    private static final String MODEL_NAME = "g_picture";

    @Override
    public int insertPictures(Picture picture, MultipartFile[] files) {
        log.info("开始上传图片");
        String path = ImageUtil.upload(files, MODEL_NAME);
        log.info("图片组上传成功，封面图片为{}", path);
        picture.setOriginalImg(path);
        picture.setRealImg(path);
        picture.setThumbImg(path);
        picture.setAuthor(UserContext.user().getUserName());
        picture.setIsMaster(true);
        picture.setCreateTime(DateUtil.nowDateTime());
        picture.setUpdateTime(DateUtil.nowDateTime());
        picture.setUpdateBy(UserContext.user().getUserName());
        picture.setCreateBy(UserContext.user().getUserName());
        log.info("图片对象存储{}", picture);
        return pictureMapper.insert(picture);
    }


    @Override
    public int updateStatusById(long id, boolean status) {
        Picture picture = new Picture();
        picture.setPictureId(id);
        picture.setStatus(status);
        log.info("修改图片{}启用状态为{}", id, status);
        return pictureMapper.updateById(picture);
    }

    @Override
    public PageInfo pageInfo(Object target) {
        QueryWrapper <Picture> wrapper = new QueryWrapper <>();
        wrapper.setEntity((Picture) target);
        PageInfo <Picture> picturePageInfo = new PageInfo <>(pictureMapper.selectList(wrapper));
        log.info("列表分页查询成功{}", picturePageInfo.getList());
        return picturePageInfo;
    }
}
